package com.ruoyi.pos.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pos.domain.print.PosPrintScheme;
import com.ruoyi.pos.mapper.print.PosPrintSchemeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * POS打印方案Controller
 * 
 * @author ruoyi
 * @date 2025-01-30
 */
@Api(tags = "POS打印方案管理")
@RestController
@RequestMapping("/pos/print")
public class PosPrintSchemeController extends BaseController {

    @Autowired
    private PosPrintSchemeMapper posPrintSchemeMapper;

    /**
     * 查询打印方案列表
     */
    @ApiOperation("查询打印方案列表")
    @PreAuthorize("@ss.hasPermi('pos:print:list')")
    @GetMapping("/list")
    public TableDataInfo list(PosPrintScheme posPrintScheme) {
        startPage();
        List<PosPrintScheme> list = posPrintSchemeMapper.selectPosPrintSchemeList(posPrintScheme);
        return getDataTable(list);
    }

    /**
     * 导出打印方案列表
     */
    @ApiOperation("导出打印方案列表")
    @PreAuthorize("@ss.hasPermi('pos:print:export')")
    @Log(title = "POS打印方案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PosPrintScheme posPrintScheme) {
        List<PosPrintScheme> list = posPrintSchemeMapper.selectPosPrintSchemeList(posPrintScheme);
        ExcelUtil<PosPrintScheme> util = new ExcelUtil<PosPrintScheme>(PosPrintScheme.class);
        util.exportExcel(response, list, "打印方案数据");
    }

    /**
     * 获取打印方案详细信息
     */
    @ApiOperation("获取打印方案详细信息")
    @PreAuthorize("@ss.hasPermi('pos:print:query')")
    @GetMapping(value = "/{schemeId}")
    public AjaxResult getInfo(@ApiParam("方案ID") @PathVariable Long schemeId) {
        return success(posPrintSchemeMapper.selectPosPrintSchemeById(schemeId));
    }

    /**
     * 新增打印方案
     */
    @ApiOperation("新增打印方案")
    @PreAuthorize("@ss.hasPermi('pos:print:add')")
    @Log(title = "POS打印方案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PosPrintScheme posPrintScheme) {
        // TODO: 调用Service的insertPosPrintScheme方法
        return error("功能开发中，请联系管理员");
    }

    /**
     * 修改打印方案
     */
    @ApiOperation("修改打印方案")
    @PreAuthorize("@ss.hasPermi('pos:print:edit')")
    @Log(title = "POS打印方案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PosPrintScheme posPrintScheme) {
        // TODO: 调用Service的updatePosPrintScheme方法
        return error("功能开发中，请联系管理员");
    }

    /**
     * 删除打印方案
     */
    @ApiOperation("删除打印方案")
    @PreAuthorize("@ss.hasPermi('pos:print:remove')")
    @Log(title = "POS打印方案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{schemeIds}")
    public AjaxResult remove(@ApiParam("方案ID数组") @PathVariable Long[] schemeIds) {
        // TODO: 调用Service的deletePosPrintSchemeBySchemeIds方法
        return error("功能开发中，请联系管理员");
    }

    /**
     * 验证打印方案
     */
    @ApiOperation("验证打印方案")
    @PreAuthorize("@ss.hasPermi('pos:print:validate')")
    @PostMapping("/validate")
    public AjaxResult validate(@RequestBody PosPrintScheme posPrintScheme) {
        // TODO: 实现打印方案验证
        return success("打印方案验证通过");
    }

    /**
     * 获取打印方案预览
     */
    @ApiOperation("获取打印方案预览")
    @PreAuthorize("@ss.hasPermi('pos:print:query')")
    @PostMapping("/preview/{schemeId}")
    public AjaxResult preview(@ApiParam("方案ID") @PathVariable Long schemeId, 
                             @RequestBody(required = false) Map<String, Object> previewData) {
        // TODO: 实现预览生成
        return success("<html><body>预览功能开发中</body></html>");
    }

    /**
     * 复制打印方案
     */
    @ApiOperation("复制打印方案")
    @PreAuthorize("@ss.hasPermi('pos:print:add')")
    @Log(title = "复制打印方案", businessType = BusinessType.INSERT)
    @PostMapping("/copy/{schemeId}")
    public AjaxResult copy(@ApiParam("方案ID") @PathVariable Long schemeId, 
                          @RequestBody Map<String, Object> params) {
        // TODO: 实现复制打印方案
        return error("功能开发中，请联系管理员");
    }

    /**
     * 设置默认打印方案
     */
    @ApiOperation("设置默认打印方案")
    @PreAuthorize("@ss.hasPermi('pos:print:edit')")
    @Log(title = "设置默认打印方案", businessType = BusinessType.UPDATE)
    @PostMapping("/setDefault")
    public AjaxResult setDefault(@RequestBody Map<String, Object> params) {
        // TODO: 实现设置默认打印方案
        return error("功能开发中，请联系管理员");
    }

    /**
     * 获取默认打印方案
     */
    @ApiOperation("获取默认打印方案")
    @PreAuthorize("@ss.hasPermi('pos:print:query')")
    @GetMapping("/default/{schemeType}")
    public AjaxResult getDefault(@ApiParam("方案类型") @PathVariable String schemeType) {
        // TODO: 实现获取默认打印方案
        PosPrintScheme defaultScheme = new PosPrintScheme();
        defaultScheme.setSchemeType(schemeType);
        List<PosPrintScheme> list = posPrintSchemeMapper.selectPosPrintSchemeList(defaultScheme);
        if (list != null && !list.isEmpty()) {
            return success(list.get(0));
        }
        return success(null);
    }
}
